<?php
require_once('../model/m_users.php');

class maintenance_users{

	public $data = array();
	public $iduser;
	public $fullname;
	public $password;
	public $enabled;
	public $idrole;
	
	public function actionView(){
		$this->data = getUsuarios();
		include_once("../views/v_session.php");	
		include_once("../views/v_header_principal_lev1.html");
		include_once("../views/v_maintenance_users.html");
		include_once("../views/v_footer_principal.html");
	}
	
	public function actionAdd(){
		$this->data= getRoles();
		include_once("../views/v_session.php");		
		include_once("../views/v_header_principal_lev1.html");
		include_once("../views/v_maintenance_users_add.html");
		include_once("../views/v_footer_principal.html");
	}

	public function actionAddUser(){	
		if(addUser($this->iduser,
				   $this->fullname,
				   $this->password,
				   $this->enabled,
				   $this->idrole)){
			header("location:c_maintenance_users.php?view=list");
			exit;				
		} else {		
			echo "<h1> Error creating User</h1>";
			exit;
		}	
	}	

	public function actionEditRole($user_id){		
		$current_user = array();
		$current_user = getUser($user_id);
		$this->data= getRoles();
		
		///*** FALTA MANEJAR User ID INEXISTENTE
		include_once("../views/v_session.php");		
		include_once("../views/v_header_principal_lev1.html");
		include_once("../views/v_maintenance_users_edit.html");
		include_once("../views/v_footer_principal.html");
	}	

	public function actionSaveUserEdit(){
		
		if(saveUserEdit($this->iduser,
				   $this->fullname,
				   $this->password,
				   $this->enabled,
				   $this->idrole)){
			header("location:c_maintenance_users.php?view=list");
			exit;
		}
		else {
			include_once("../views/v_session.php");		
			include_once("../views/v_header_principal_lev1.html");
			echo "<h1>THERE WAS A PROBLEM WHEN TRYING TO SAVE THE CHANGES!!!</h1>";
			include_once("../views/v_footer_principal.html");
		}	
	
	}
	
}

$instancia= new maintenance_users;

if(isset($_GET['view'])){
	if($_GET['view']=="list")	
		$instancia->actionView();
}

if(isset($_GET['add'])){
	if($_GET['add']=="go")	
		$instancia->actionAdd();
}

if(isset($_GET['user_name'])){
		$instancia->iduser =($_GET['user_name']);
		$instancia->fullname =($_GET['full_name']);
		$instancia->password =($_GET['pass_word']);
		$instancia->enabled =($_GET['enabled']);
		$instancia->idrole =($_GET['role']);
		
		$instancia->actionAddUser();
}

if(isset($_GET['edit'])){
		$instancia->actionEditRole($_GET['edit']);
}

if(isset($_GET['save_user'])){
		$instancia->iduser =($_GET['save_user']);
		$instancia->fullname =($_GET['full_name']);
		$instancia->password =($_GET['pass_word']);
		$instancia->enabled =($_GET['enabled']);
		$instancia->idrole =($_GET['role']);

		$instancia->actionSaveUserEdit();
}

?>